package com.jinyu.jyly.ui.community;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.jinyu.jyly.R;
import com.jinyu.jyly.adapter.CommunityListAdapter;
import com.jinyu.jyly.base.BaseFragment;
import com.jinyu.jyly.bean.AllInfoBean;
import com.jinyu.jyly.bean.CommunityListInfo;
import com.jinyu.jyly.bean.MineUserInfo;
import com.jinyu.jyly.utils.AES;


import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 我的社区
 */
public class CommunityFragment extends BaseFragment<CommunityPresenter> implements CommunityContract.View {

    @BindView(R.id.fake_status_bar)
    View mFakeStatusBar;
    @BindView(R.id.recycleview)
    RecyclerView mRecycleview;
    @BindView(R.id.cl_empty)
    ConstraintLayout rl_empty;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;
    @BindView(R.id.tv_community_num)
    TextView tv_community_num;
  @BindView(R.id.tv_share_num)
    TextView tv_share_num;

    private CommunityPresenter mPresenter;

    @Override
    protected View initView(ViewGroup container) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.fragment_community, container, false);
        return view;
    }

    @Override
    protected void loadData() {
        mRecycleview.setLayoutManager(new LinearLayoutManager(getActivity()));
        mPresenter.getChildsList();

        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mPresenter.getUserView();
                mPresenter.getChildsList();
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.getUserView();
    }

    @Override
    public CommunityPresenter getPresenter() {
        if(mPresenter == null){
            mPresenter = new CommunityPresenter(getActivity(), this);
        }
        return mPresenter;
    }


    @Override
    public void onFailer(Throwable throwable) {
        mRefreshLayout.finishLoadMore();
        mRefreshLayout.finishRefresh();
    }

    /**
     * 获取直推
     * @param infoBean
     */
    @Override
    public void onChildsList(AllInfoBean infoBean) {
        mRefreshLayout.finishLoadMore();
        mRefreshLayout.finishRefresh();
        switch (infoBean.getStatus()){
            case 200:
                String data = infoBean.getData();
                try {
                    String result = AES.decrypt(data);
                    Gson gson = new Gson();
                    Type listType = new TypeToken<ArrayList<CommunityListInfo>>() {}.getType();
                    List<CommunityListInfo> mList = gson.fromJson(result, listType);
                    if(mList != null && mList.size() != 0){
                        rl_empty.setVisibility(View.GONE);
                        mRecycleview.setVisibility(View.VISIBLE);
                        CommunityListAdapter communityListAdapter = new CommunityListAdapter(R.layout.item_community_list, mList);
                        mRecycleview.setAdapter(communityListAdapter);
                    }else{
                        rl_empty.setVisibility(View.VISIBLE);
                        mRecycleview.setVisibility(View.GONE);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case 422:
                showToast(infoBean.getMsg());
                break;
            default:break;
        }
    }

    /**
     *
     * @param infoBean
     */
    @Override
    public void onGetUserView(AllInfoBean infoBean) {
        mRefreshLayout.finishLoadMore();
        mRefreshLayout.finishRefresh();
        switch (infoBean.getStatus()){
            case 200:
                String beanData = infoBean.getData();
                String result = null;
                try {
                    result = AES.decrypt(beanData);
                    Gson gson = new Gson();
                    MineUserInfo mineUserInfo = gson.fromJson(result, MineUserInfo.class);
                    tv_community_num.setText(mineUserInfo.getPerformance()+"人");
                    tv_share_num.setText(mineUserInfo.getZhi_num()+"人");
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case 422:
                showToast(infoBean.getMsg());
                break;
            default:break;
        }
    }


}
